package com.sfang.common.base.exception;

import com.sfang.common.base.enums.BaseEnums;
import com.sfang.common.base.enums.SystemEnum;


/**
 * 系统错误处理
 */
public class BusinessException extends RuntimeException implements BaseException {

    private static final long serialVersionUID = 1L;

    protected String message;
    protected int code;

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(int code, Throwable cause) {
        super(cause);
        this.code = code;
    }

    public BusinessException(BaseEnums enums) {
        super(enums.getMsg());
        this.message = enums.getMsg();
        this.code = enums.getCode();
    }

    public BusinessException(BaseEnums enums, Exception e) {
        super(enums.getMsg(), e);
        this.message = enums.getMsg();
        this.code = enums.getCode();
    }

    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public BusinessException(int code, String format, Object... args) {
        super(String.format(format, args));
        this.code = code;
        this.message = String.format(format, args);
    }

    public static BusinessException wrap(int code, String message, Object... args) {
        return new BusinessException(code, message, args);
    }

    public static BusinessException wrap(String message, Object... args) {
        return new BusinessException(SystemEnum.SYSTEM_BUSY.getCode(), message, args);
    }

    public static BusinessException validFail(String message, Object... args) {
        return new BusinessException(SystemEnum.BASE_VALID_PARAM.getCode(), message, args);
    }

    public static BusinessException wrap(BaseEnums ex) {
        return new BusinessException(ex.getCode(), ex.getMsg());
    }

    public String getMessage() {
        return this.message;
    }

    public int getCode() {
        return this.code;
    }

    public String toString() {
        return "BusinessException [message=" + this.message + ", code=" + this.code + "]";
    }
}
